package cn.com.lyj6851.modules.edu.service.impl;


import cn.com.lyj6851.common.service.impl.BaseServiceImpl;
import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.enums.ProductEnum;
import cn.com.lyj6851.enums.ShopCommonEnum;
import cn.com.lyj6851.modules.edu.domain.EduChapter;
import cn.com.lyj6851.modules.edu.domain.EduCourse;
import cn.com.lyj6851.modules.edu.domain.EduCourseDescription;
import cn.com.lyj6851.modules.edu.domain.EduVideo;
import cn.com.lyj6851.modules.edu.service.EduChapterService;
import cn.com.lyj6851.modules.edu.service.EduCourseDescriptionService;
import cn.com.lyj6851.modules.edu.service.EduCourseService;
import cn.com.lyj6851.modules.edu.service.EduVideoService;
import cn.com.lyj6851.modules.edu.service.mapper.EduCourseMapper;
import cn.com.lyj6851.modules.edu.service.mapper.EduSubjectMapper;
import cn.com.lyj6851.modules.edu.vo.CourseInfoVo;
import cn.com.lyj6851.modules.edu.vo.EduSubjectVo;
import cn.com.lyj6851.modules.product.domain.YdyStoreProductRelation;
import cn.com.lyj6851.modules.product.service.YdyStoreProductRelationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.var;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class EduCourseServiceImpl extends BaseServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    private final IGenerator generator;
    private final EduCourseMapper eduCourseMapper;
    private final EduSubjectMapper eduSubjectMapper;
    private final EduCourseDescriptionService eduCourseDescriptionService;
    private final EduChapterService eduChapterService;
    private final EduVideoService eduVideoService;
    private final YdyStoreProductRelationService ydyStoreProductRelationService;


    /**精品推荐
     * 首发新品
     * 猜你喜欢
     * 热门榜单
     * */
    @Override
    public List<CourseInfoVo> getList(int page, int limit, int order) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(EduCourse::getIsShow, ShopCommonEnum.SHOW_1.getValue()).orderByDesc(EduCourse::getSort);

        switch (ProductEnum.toType(order)) {
            case TYPE_1:
                wrapper.lambda().eq(EduCourse::getIsBest,
                        ShopCommonEnum.IS_STATUS_1.getValue());
                break;
            case TYPE_3:
                wrapper.lambda().eq(EduCourse::getIsNew,
                        ShopCommonEnum.IS_STATUS_1.getValue());
                break;
            case TYPE_4:
                wrapper.lambda().eq(EduCourse::getIsBenefit,
                        ShopCommonEnum.IS_STATUS_1.getValue());
                break;
            case TYPE_2:
                wrapper.lambda().eq(EduCourse::getIsHot,
                        ShopCommonEnum.IS_STATUS_1.getValue());
                break;
            case TYPE_5:
                wrapper.lambda().eq(EduCourse::getIsGood,
                        ShopCommonEnum.IS_STATUS_1.getValue());
                break;
            default:
        }
        Page<EduCourse> pageModel = new Page<>(page, limit);

        IPage<EduCourse> pageList = eduCourseMapper.selectPage(pageModel, wrapper);
        List<EduCourse> courseList = pageList.getRecords();
        courseList.forEach(ec -> {
            ec.setEduSubjectVo(new EduSubjectVo(eduSubjectMapper.selectById(ec.getSubjectId())));
        });
        return generator.convert(courseList, CourseInfoVo.class);
    }

    @Override
    public CourseInfoVo courseDetail(Long id) {
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        EduCourse eduCourse = baseMapper.selectById(id);
        EduCourseDescription courseDesc = eduCourseDescriptionService.getById(id);
        if(eduCourse != null){
            BeanUtils.copyProperties(eduCourse, courseInfoVo);
        }
        // 课程简介
        courseInfoVo.setDescription(courseDesc.getDescription());
        List<EduChapter> eduChapterList = eduChapterService.list(Wrappers.<EduChapter>lambdaQuery()
                .eq(EduChapter::getCourseId,id));
        // 课程章节
        courseInfoVo.setEduChapter(eduChapterList);

        Map<String,Object> map = new LinkedHashMap<>();
        List<EduVideo> eduVideoList = eduVideoService.list(Wrappers.<EduVideo>lambdaQuery()
                .eq(EduVideo::getCourseId,id)
                .eq(EduVideo::getIsDel,ShopCommonEnum.DELETE_0.getValue()));
        // 课程小节
        eduChapterList.forEach(x->{
            map.put(x.getId(),eduVideoList.stream().filter(y->y.getChapterId().equals(x.getId())).collect(Collectors.toList()));
        });
        courseInfoVo.setEduVideos(map);
        final var one = ydyStoreProductRelationService.getOne(Wrappers.<YdyStoreProductRelation>lambdaQuery()
                .eq(YdyStoreProductRelation::getProductId, id));
        if(one!=null){
            courseInfoVo.setType(one.getType());
        }

        return courseInfoVo;
    }

    /**
     * 浏览量+1
     * @param courseId
     */
    @Override
    public void incBrowseNum(Long courseId) {
        eduCourseMapper.incBrowseNum(courseId);
    }


}
